package io.renren.modules.wms.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.sys.service.CacheService;
import io.renren.modules.wms.dao.DailyTempDao;
import io.renren.modules.wms.entity.DailyTempEntity;
import io.renren.modules.wms.entity.PictureInfoEntity;
import io.renren.modules.wms.service.DailyTempService;
import io.renren.modules.wms.service.PictureInfoService;
import io.renren.modules.wms.service.WeiboContentService;
import io.renren.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("dailyTempService")
public class DailyTempServiceImpl extends ServiceImpl<DailyTempDao, DailyTempEntity> implements DailyTempService {

    @Autowired
    private WeiboContentService weiboContentService;

    @Autowired
    private PictureInfoService pictureInfoService;

    @Resource
    private CacheService cacheService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DailyTempEntity> page = this.page(
                new Query<DailyTempEntity>().getPage(params),
                new QueryWrapper<>()
        );
        List<Integer> idList = (List<Integer>) params.get("idList");
        List<DailyTempEntity> list = this.list();
        if(ValidationUtil.isEmptyList(list)){
            R r = weiboContentService.genAutoTemp(idList);
            if(String.valueOf(r.get("code")).equals("0")) {
                List<DailyTempEntity> resList = (List<DailyTempEntity>) r.get("data");
                setPics(resList);
                page.setRecords(resList);
            }
        } else {
            setPics(list);
            page.setRecords(list);
        }
        return new PageUtils(page);
    }

    @Override
    public void removeAll() {
        List<DailyTempEntity> list = this.list();
        if(ValidationUtil.isEmptyList(list)){
            return;
        }
        List<Integer> collect = list.stream().map(DailyTempEntity::getId).collect(Collectors.toList());
        this.removeByIds(collect);
    }

    /**
     * 构造一整条微博的实体
     * @param records
     */
    private void setPics(List<DailyTempEntity> records){
        for(DailyTempEntity dailyTempEntity: records){
            String picIdList = dailyTempEntity.getPicIdList();
            if(ValidationUtil.isBlank(picIdList)){
                continue;
            }
            List<String> collect = Arrays.asList(picIdList.split(","));
            if(ValidationUtil.isEmptyList(collect)){
                continue;
            }
            List<String> picPathList = new ArrayList<>();
            for(String picId : collect){
                PictureInfoEntity pic = pictureInfoService.getById(Integer.parseInt(picId));
                picPathList.add("http://" + pic.getHttpUrl());
            }
            dailyTempEntity.setPicPathList(picPathList);
        }
    }

}